package com.kehao.leetcode.order;

/**
 * @author Kehao.Chen
 * @version 1.0
 * @date 2022/6/10 17:08
 */
public class LeetcodeSolution114 {


    private static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }


    public void flatten(TreeNode root) {
        if(root==null){
            return;
        }
        TreeNode left = root.left;
        TreeNode right = root.right;
        if(left!=null){
            flatten(left);
            root.right = left;
            root.left = null;
            while (left.right!=null){
                left = left.right;
            }
        }
        if(right!=null){
            flatten(right);
            if(left!=null){
                left.right = right;
            }
        }
    }

}
